package com.ruoyi.service.impl;

import com.github.pagehelper.PageInfo;
import com.hundun.vision.api.common.PlainResult;
import com.hundun.vision.api.dto.ExchangeDTO;
import com.hundun.vision.api.dto.RobotDTO;
import com.hundun.vision.api.dto.StrategyDTO;
import com.hundun.vision.api.query.RobotQuery;
import com.hundun.vision.api.request.RobotRequest;
import com.hundun.vision.api.service.RPCExchangeService;
import com.hundun.vision.api.service.RPCRobotService;
import com.hundun.vision.api.service.RPCStrategyService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ResultUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.service.RobotService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ：RuoChen
 * @date ：16:28 2021/6/10
 * @description：机器人服务实现类
 */
@Slf4j
@Service
public class RobotServiceImpl implements RobotService {

    @DubboReference(timeout = 60000, retries = 0)
    private RPCRobotService robotService;

    @DubboReference(timeout = 60000, retries = 0)
    private RPCExchangeService exchangeService;

    @DubboReference(timeout = 60000, retries = 0)
    private RPCStrategyService strategyService;

    @Override
    public Boolean save(RobotRequest request) {
        log.info("save robot,request:{}", request);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        request.setUserId(user.getUserId());
        PlainResult<Boolean> result = robotService.save(request);
        if (result.isSuccess()) {
            return result.getData();
        } else {
            ResultUtils.error(result.getMessage());
        }
        return false;
    }

    @Override
    public Integer removeByIds(List<Long> robotIds) {
        log.info("remove robot by ids:{}", robotIds);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        PlainResult<Integer> result = robotService.removeByIds(user.getUserId(), robotIds);
        if (result.isSuccess()) {
            return result.getData();
        } else {
            ResultUtils.error(result.getMessage());
        }
        return 0;
    }

    @Override
    public Boolean update(RobotRequest request) {
        log.info("update robot,request:{}", request);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        request.setUserId(user.getUserId());
        PlainResult<Boolean> result = robotService.update(request);
        if (result.isSuccess()) {
            return result.getData();
        } else {
            ResultUtils.error(result.getMessage());
        }
        return false;
    }

    @Override
    public List<ExchangeDTO> getExchanges() {
        log.info("get exchanges");
        PlainResult<List<ExchangeDTO>> result = exchangeService.list();
        if (result.isSuccess()) {
            return result.getData();
        } else {
            ResultUtils.error(result.getMessage());
        }
        return null;
    }

    @Override
    public List<StrategyDTO> getStrategies() {
        log.info("get strategies");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        PlainResult<List<StrategyDTO>> result = strategyService.getByUserId(user.getUserId());
        if (result.isSuccess()) {
            return result.getData();
        } else {
            ResultUtils.error(result.getMessage());
        }
        return null;
    }

    @Override
    public RobotDTO getById(Long robotId) {
        log.info("get robot by id,robotId:{}", robotId);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        PlainResult<RobotDTO> result = robotService.getById(user.getUserId(), robotId);
        if (result.isSuccess()) {
            return result.getData();
        } else {
            ResultUtils.error(result.getMessage());
        }
        return null;
    }

    @Override
    public PageInfo<RobotDTO> search(RobotQuery query) {
        log.info("search robot,query:{}", query);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        query.setUserId(user.getUserId());
        PlainResult<PageInfo<RobotDTO>> result = robotService.search(query);
        if (result.isSuccess()) {
            return result.getData();
        } else {
            ResultUtils.error(result.getMessage());
        }
        return null;
    }

    @Override
    public Integer start(List<Long> robotIds) {
        log.info("get robot by ids,robotIds:{}", robotIds);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        PlainResult<Integer> result = robotService.start(user.getUserId(), robotIds);
        if (result.isSuccess()) {
            return result.getData();
        } else {
            ResultUtils.error(result.getMessage());
        }
        return 0;
    }

    @Override
    public Integer pause(List<Long> robotIds) {
        log.info("get robot by ids,robotIds:{}", robotIds);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        PlainResult<Integer> result = robotService.pause(user.getUserId(), robotIds);
        if (result.isSuccess()) {
            return result.getData();
        } else {
            ResultUtils.error(result.getMessage());
        }
        return 0;
    }
}
